﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar.Core.ExtMethod;

namespace Kugar.Core.WinForm.ExtMethod
{
    public  static  class DialogExt
    {
        /// <summary>
        ///     根据格式,添加扩展名,省去了记住Filter的格式
        /// </summary>
        /// <param name="dialog"></param>
        /// <param name="extName">添加的扩展名的名称</param>
        /// <param name="ext">添加的扩展名扩展名(省略"*.")</param>
        /// <returns></returns>
        /// <example>
        ///     dialog.AppendFilter("PDF 文件", "pdf").AppendFilter("所有文件", "*");
        /// </example>
        public static FileDialog AppendFilter(this FileDialog dialog, string extName, string ext)
        {
            //"PDF files (*.pdf)|*.pdf|All files (*.*)|*.*";
            var str = dialog.Filter;

            if (string.IsNullOrWhiteSpace(str))
            {
                str = string.Format("{0} files (*.{1})|*.{1}", extName, ext);
            }
            else
            {
                if (str.EndsWith("|"))
                {
                    str += (string.Format("{0} files (*.{1})|*.{1}", extName, ext));
                }
                else
                {
                    str += (string.Format("|{0} files (*.{1})|*.{1}", extName, ext));
                }
            }

            dialog.Filter = str;

            return dialog;
        }

        private static string _lastSaveFilePath = Application.StartupPath;
        private static string _lastOpenFilePath = Application.StartupPath;

        /// <summary>
        ///     打开一个保存文件的对话框,如果有选择保存的路径则返回保存路径,如果未选择,则返回空字符串
        /// </summary>
        /// <param name="filter">扩展名的字符串,格式为: "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*" ,如果为空,则使用所有扩展名(All files (*.*)|*.*)</param>
        /// <param name="defaultFileName">默认的文件名,如果为空,则默认使用 "未命名"</param>
        /// <param name="initPath">初始化路径,如果为空,则使用程序上一次选择的路径</param>
        /// <returns>如果有选中路径,则返回文件路径,如果没有选中,则返回空字符串</returns>
        public static string ShowSaveFileDialog(string filter = "", string defaultFileName = "", string initPath = "")
        {
            using (var dialog = CreateSaveFileDialog(filter, defaultFileName, initPath))
            {
                if (dialog.ShowDialog()==DialogResult.OK)
                {
                    _lastSaveFilePath =Path.GetDirectoryName(Path.GetFullPath(dialog.FileName));
                    return dialog.FileName;
                }
                else
                {
                    return string.Empty;
                }                
            }


        }

        /// <summary>
        ///     打开一个打开文件文件的对话框,如果有选择保存的路径则返回保存路径,如果未选择,则返回空字符串
        /// </summary>
        /// <param name="filter">扩展名的字符串,格式为: "PDF files (*.pdf)|*.pdf|All files (*.*)|*.*" ,如果为空,则使用所有扩展名(All files (*.*)|*.*)</param>
        /// <param name="defaultFileName">默认的文件名,如果为空,则默认使用 "未命名"</param>
        /// <param name="initPath">初始化路径,如果为空,则使用程序上一次选择的路径</param>
        /// <param name="isCheckFileExists">是否强制检查选中文件是否存在,true为文件必须存在,false为不检查</param>
        /// <returns>如果有选中路径,则返回文件路径,如果没有选中,则返回空字符串</returns>
        public static string ShowOpenFileDialog(string filter = "", string defaultFileName = "", string initPath = "",
                                                bool isCheckFileExists = true)
        {
            using (var dialog = CreateOpenFileDialog(filter, defaultFileName, initPath, isCheckFileExists))
            {
                if (dialog.ShowDialog() == DialogResult.OK)
                {
                    _lastOpenFilePath =Path.GetDirectoryName(Path.GetFullPath(dialog.FileName));
                    return dialog.FileName;
                }
                else
                {
                    return string.Empty;
                }                
            }


        }

        public static SaveFileDialog CreateSaveFileDialog(string filter="",string defaultFileName="", string initPath="")
        {
            var temp = new SaveFileDialog();

            temp.AddExtension = false;
            temp.FileName = defaultFileName.IfEmptyOrWhileSpace("未命名");
            temp.Filter = filter.IfEmptyOrWhileSpace("All files (*.*)|*.*").Trim('|');
            temp.InitialDirectory = initPath.IfEmptyOrWhileSpace(_lastSaveFilePath);

            return temp;

        }

        public static OpenFileDialog CreateOpenFileDialog(string filter="", string defaultFileName="", string initPath="",
                                                          bool isCheckFileExists=true)
        {
            var temp = new OpenFileDialog();

            temp.AddExtension = false;
            temp.FileName = defaultFileName.IfEmptyOrWhileSpace("未命名");
            temp.Filter = filter.IfEmptyOrWhileSpace("All files (*.*)|*.*").Trim('|');
            temp.InitialDirectory = initPath.IfEmptyOrWhileSpace(_lastOpenFilePath);
            temp.CheckFileExists = isCheckFileExists;

            return temp;

        }
    }
}
